package com.mbbb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mbbb.constant.MessageConstant;
import com.mbbb.pojo.OrderSetting;
import com.mbbb.service.OrderSettingService;
import com.mbbb.util.POIUtils;
import com.mbbb.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderSetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody OrderSetting orderSetting){ // get请求，无须使用@RequestBody
        try {
            orderSettingService.saveOrUpdate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map<String, Integer>> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile){

        try {
            List<String[]> arrayList = POIUtils.readExcel(excelFile);
            //数据类型转换，将集合String[]类型装换成OrderSetting对象类型
            List<OrderSetting> dataList = new ArrayList<OrderSetting>();
            for (String[] arrs : arrayList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(arrs[0]));
                orderSetting.setNumber(Integer.parseInt(arrs[1]));
                orderSetting.setReservations(0);
                dataList.add(orderSetting);
            }

            orderSettingService.addBatch(dataList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

}
